<?php
declare (strict_types=1);

namespace app\promotion\model;

use app\common\model\BaseModel;
use app\course\logic\base\Course;
use think\facade\Request;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;
use think\model\relation\HasOne;

/**
 * 活动模型
 */
class Promotion extends BaseModel
{
    use softDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'promotion';

    protected $autoWriteTimestamp = true;

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'          => 'integer',
        'price'       => 'float',
        'discount'    => 'float',
        'num'         => 'integer',
        'start_time'  => 'integer',
        'end_time'    => 'integer',
        'create_time' => 'integer',
    ];

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    // protected $visible = ['id', 'type', 'start_time', 'end_time', 'price', 'discount', 'num'];

    protected $hidden = ['product_type_id', 'status', 'create_time', 'update_time', 'delete_time'];

    /**
     * 获取默认隐藏字段
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-12
     *
     * @return string[] [type] [description]
     */
    public function getHiddenColumn(): array
    {
        return $this->hidden;
    }

    /**
     * 开始时间查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-06
     *
     * @param  [type] $query [description]
     */
    public function scopeStartTime($query)
    {
        $query->where('start_time', '<', time());
    }

    /**
     * 结束时间查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-06
     *
     * @param  [type] $query [description]
     */
    public function scopeEndTime($query)
    {
        $query->where('end_time', '>', time());
    }

    /**
     * 活动状态查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-06
     *
     * @param  [type]  $query  [description]
     * @param integer $status [description]
     */
    public function scopeStatus($query, int $status = 1)
    {
        $query->where('status', $status);
    }

    /**
     * 产品查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-06
     *
     * @param $query
     * @param $product
     */
    public function scopeProduct($query, $product)
    {
        $query->whereProductId($product);
    }

    /**
     * 产品类型查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-06
     *
     * @param $query
     * @param $product_type
     */
    public function scopeProductType($query, $product_type)
    {
        $query->whereProductTypeId($product_type);
    }

    /**
     * 产品类型查询范围
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-06
     *
     * @param  [type]  $query  [description]
     */
    public function scopeCurrent($query)
    {
        $query->where('start_time', '<', time())->where('end_time', '>', time())->where('status', 1);
    }

    /**
     * 活动进行状态获取器
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-16
     *
     * @param string $value [description]
     * @param  [type] $data  [description]
     * @return int [type]        [description]
     */
    public function getRunningStatusAttr($value, $data): int
    {
        $time = Request::time();
        return isset($data['start_time']) ? ($data['start_time'] > $time ? 0 : 1) : 0;
    }

    /**
     * 活动开始倒计时获取器
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-16
     *
     * @param string $value [description]
     * @param        $data
     * @return mixed [type]        [description]
     */
    public function getStartCountdownAttr($value, $data)
    {
        return isset($data['start_time']) ? max($data['start_time'] - time(), 0) : 0;
    }

    /**
     * 活动结束倒计时获取器
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-16
     *
     * @param string $value [description]
     * @param        $data
     * @return mixed [type]        [description]
     */
    public function getEndCountdownAttr($value, $data)
    {
        return isset($data['end_time']) ? max($data['end_time'] - time(), 0) : 0;
    }

    /**
     * 活动类型名称
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-06
     *
     * @param string $value [description]
     * @param        $data
     * @return string [type]        [description]
     */
    public function getTypeTextAttr($value, $data): string
    {
        $type_text = self::getTypeText();

        return isset($data['type']) ? ($type_text[$data['type']] ?? '') : '';
    }

    /**
     * 活动名称
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-12
     *
     * @return string[] [type] [description]
     */
    public static function getTypeText(): array
    {
        return [
            1 => '限时折扣',
            2 => '秒杀',
            3 => '砍价',
            4 => '拼团',
        ];
    }

    /**
     * 发起者砍价详情信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-12
     *
     * @return mixed [type] [description]
     */
    public function bargained()
    {
        return $this->hasOne(PromotionBargain::class)->with(['bargain_data'])->sponsor()
            ->bind(['bargained_nums', 'bargained_total_price', 'bargain_finished' => 'done', 'bargain_data']);
    }

    /**
     * 砍价详情信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-12
     *
     * @return HasOne [type] [description]
     */
    public function bargain(): HasOne
    {
        return $this->hasOne(PromotionBargain::class)// ->bind(['bargained_nums', 'bargained_total_price', 'bargain_finished' => 'done'])
            ;
    }

    /**
     * 关联产品信息
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-12
     *
     * @return BelongsTo|void [type] [description]
     */
    public function productInfo()
    {
        $data = $this->getData();

        switch ($data['product_type_id']) {
            case 1:
            case 2:
            case 3:
            case 4:
                // 课程
                return $this->belongsTo(Course::class, 'product_id')->with(['cover_url'])->append(['course_type_text'])->bind([
                    'product_title'     => 'title',
                    'product_type'      => 'course_type',
                    'product_type_text' => 'course_type_text',
                    'product_price'     => 'price',
                    'product_cover'     => 'cover_url',
                ]);

            default:
                # code...
                break;
        }
    }

    /**
     * 团购数据
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-14
     *
     * @return HasOne [type] [description]
     */
    public function tuan(): HasOne
    {
        return $this->hasOne(PromotionTuan::class);
    }
}
